package com.gs.m_person_details

import android.content.Context
import android.os.Bundle
import android.telephony.PhoneNumberUtils
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.ImageView
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import com.gs.common.BaseCompatActivity
import com.gs.common.global.GlobalVariables
import com.gs.common.util.ActionBarUtil
import com.gs.common.util.CheckPhoneNumUtil
import com.gs.common.util.ToastUtil
import com.gs.common.util.UnderlineUtil
import com.gs.m_person_details.databinding.ChangeInfoLayoutBinding
import com.gs.m_person_details.persistent.ChangeInfoPersistent

class ChangeInfoActivity: BaseCompatActivity(), ChangeInfoPersistent.OnDoUpdateUserInfoStateCallback {

    private lateinit var binding: ChangeInfoLayoutBinding
    private val changeInfoPersistent by lazy { ChangeInfoPersistent.getInstance(this) }
    private val mLoginUser = GlobalVariables.loginUser

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.change_info_layout)

        // 设置自定义 action bar
        ActionBarUtil.setCustomActionBar(this, R.layout.change_info_actionbar_layout)

        // 设置返回键
        val backBtn = findViewById<ImageView>(R.id.back_btn)
        backBtn.setOnClickListener {
            finish()
        }

        // 设置action bar title
        val actionBarTitle = findViewById<TextView>(R.id.action_bar_title)
        actionBarTitle.text = intent.getStringExtra("title")

        // 设置action bar 保存按键
        val saveBtn = findViewById<TextView>(R.id.save_btn)
        saveBtn.setOnClickListener {
            if (mLoginUser == null) {
                ToastUtil.showShortToast(this, "保存失败")
                return@setOnClickListener
            }
            val title = intent.getStringExtra("title")
            val changeContent = binding.changeInfoEdt.text.toString().trim()
            when (title) {
                "昵称" -> {
                    if (changeContent.length !in 1..10) {
                        ToastUtil.showShortToast(this, "昵称的字数不能小于1或者大于10")
                        return@setOnClickListener
                    }
                    if (changeContent[0].isDigit()) {
                        ToastUtil.showShortToast(this, "昵称不能以数字开头")
                        return@setOnClickListener
                    }
                    changeInfoPersistent.updateUserInfo(
                        mLoginUser.userName,
                        changeContent,
                        null,
                        null,
                        this
                    )
                }
                "用户名" -> {
                    if (changeContent.length !in 3..12) {
                        ToastUtil.showShortToast(this, "用户名的长度不能小于3或者大于12")
                        return@setOnClickListener
                    }
                    if (!changeContent[0].isLetter()) {
                        ToastUtil.showShortToast(this, "用户名必须以字母开头")
                        return@setOnClickListener
                    }
                    changeInfoPersistent.updateUserInfo(
                        mLoginUser.userName,
                        null,
                        changeContent,
                        null,
                        this
                    )
                }
                "手机号" -> {
                    if (!CheckPhoneNumUtil.isValidTelNumber(changeContent)) {
                        ToastUtil.showShortToast(this, "无效手机号")
                        return@setOnClickListener
                    }
                    changeInfoPersistent.updateUserInfo(
                        mLoginUser.userName,
                        null,
                        null,
                        changeContent,
                        this
                    )
                }
            }
        }

        UnderlineUtil.onFocusChangeColor(arrayOf(binding.changeInfoEdt), arrayOf(binding.changeInfoUnderline), com.gs.common.R.color.light_green)
    }

    override fun onResume() {
        super.onResume()
        val content = intent.getStringExtra("content")
        binding.changeInfoEdt.setText(content)
    }

    override fun onUpdateUserInfoSuccess(
        changeNickname: String?,
        changeUsername: String?,
        changePhoneNumber: String?
    ) {
        if (changeNickname != null) {
            GlobalVariables.loginUser?.nickname = changeNickname
        } else if (changeUsername != null) {
            GlobalVariables.loginUser?.userName = changeUsername
        } else if (changePhoneNumber != null) {
            GlobalVariables.loginUser?.phoneNumber = changePhoneNumber
        }
        val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(binding.changeInfoEdt.windowToken, 0)
        ToastUtil.showShortToast(this, "保存成功")
    }

    override fun onUpdateUserInfoFailed() {
        ToastUtil.showShortToast(this, "保存失败")
    }

    override fun onUsernameExistedError() {
        ToastUtil.showShortToast(this, "保存失败，用户名已存在")
    }

    override fun onPhoneNumberExistedError() {
        ToastUtil.showShortToast(this, "保存失败，手机号已存在")
    }
}